package com.mt.mtyx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.mtyx.entity.base.BaseEntity;
import com.mt.mtyx.entity.product.Attr;
import com.mt.mtyx.product.mapper.AttrMapper;
import com.mt.mtyx.product.service.AttrService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 属性业务层
 *
 * @author WMT
 * @Date 2023/7/20 15:34
 */
@Service
public class AttrServiceImpl extends ServiceImpl<AttrMapper, Attr> implements AttrService {

    @Override
    public List<Attr> getAllByGroupId(Long groupId) {
        LambdaQueryWrapper<Attr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Attr::getAttrGroupId, groupId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public boolean existsByName(String name) {
        LambdaQueryWrapper<Attr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Attr::getName, name);
        return baseMapper.exists(wrapper);
    }

    @Override
    public int insert(Attr attr) {
        LocalDateTime now = LocalDateTime.now();
        attr.setCreateTime(now);
        attr.setUpdateTime(now);
        attr.setIsDeleted(BaseEntity.NOT_DELETED);
        return baseMapper.insert(attr);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int batchDelete(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    @Override
    public int update(Attr attr) {
        attr.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(attr);
    }
}
